package com.deyuanyun.pic.ctrl.controller;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.MapUtil;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.ctrl.enumeration.EqptTypeEnum;
import com.deyuanyun.pic.ctrl.service.EqptDataOperationalService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author lijun
 * @date 2017-01-03
 * 外腐蚀控制设备数据操作
 * History: [修改人][时间][修改描述]
 */
@Controller
@RequestMapping("/corrctrl/EqptDataOperational")
public class EqptDataOperationalController {

    @Resource
    private EqptDataOperationalService eqptDataOperationalService;


    /**
     * 增加或修改设备信息
     * @param
     * @param
     */
    @RequestMapping("/insertOrReviseEqpt.html")
    public ModelAndView insertOrRevise(HttpServletRequest request) throws Exception {

        Map map = MapUtil.getParameterMap(request);
        map.put("eqptTypeName",EqptTypeEnum.getInstance((String)map.get("type")).getLabel());
        map.put("title",null != map.get("funType") && map.get("funType").equals("insert") ? "新建":"编辑");
        return new ModelAndView("/corrctrl/device/deviceCreate",map);
    }

    /**
     * 增加设备信息
     * @param
     * @param
     */
    @RequestMapping("/insert{type}.json")
    @ResponseBody
    public void insertEqptData(@PathVariable("type") String type,HttpServletRequest request) throws Exception {
        //将参数map转换为普通可操作的map
        Map map = MapUtil.getParameterMap(request);
        int num = eqptDataOperationalService.insertSelectiveEqptData(EqptTypeEnum.getInstance(type),map);
        AjaxSupport.sendSuccessText("",num);
    }

    /**
     * 得到需要修改的数据详情
     */
    @RequestMapping("/select{type}.json")
    @ResponseBody
    public void selectEqptData(@PathVariable("type") String type,String id) throws Exception {
        if(id != null && !id.equals("")){
            Object obj= eqptDataOperationalService.selectEqptData(EqptTypeEnum.getInstance(type),id);
            if(null != obj){
                AjaxSupport.sendSuccessText("",obj);
            }else{
                AjaxSupport.sendFailText("","网络错误，请重试！");
            }
        }else{
            AjaxSupport.sendFailText("","");
        }
    }

    /**
     * 更新设备信息
     * @param
     * @param
     */
    @RequestMapping("/update{type}.json")
    @ResponseBody
    public void updateEqptData(@PathVariable("type") String type,HttpServletRequest request) throws Exception {
        Map map = MapUtil.getParameterMap(request);
        if(map != null && map.get("id")!= null){
            int num = eqptDataOperationalService.updateSelectiveEqptData(EqptTypeEnum.getInstance(type),map);
            AjaxSupport.sendSuccessText("",num);
        }else{
            AjaxSupport.sendFailText("","");
        }
    }
}
